package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.GroupLinkManAdapter;
import com.yanchangapp.adapter._n_manage.LookLinkWorkAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.EmployeeBean;
import com.yanchangapp.model.bean.GroupLinkManBean;
import com.yanchangapp.model.bean.LookLinkManBean;
import com.yanchangapp.model.bean.LookLinkWorkBean;
import com.yanchangapp.model.bean.PlanWorkBean;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.model.request.MakePlanManRequest;
import com.yanchangapp.model.request.MakePlanWorkRequest;
import com.yanchangapp.ui.presenter.LookLinkWorkAtPresenter;
import com.yanchangapp.ui.view.LookLinkWorkAtView;
import com.yanchangapp.widget.SetDieTimeDialog;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择操作...
 * page: 查看关联工作项
 */
public class LookLinkWorkItemActivity extends BaseActivity<LookLinkWorkAtView, LookLinkWorkAtPresenter> implements LookLinkWorkAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.btnAllotWork)
    Button btnAllotWork;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.rvPlanItem)
    RecyclerView rvPlanItem;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;

    private LookLinkWorkAdapter mLookLinkWorkAdapter;
    private List<LookLinkWorkBean> mLookLinkWorkBeans = new ArrayList<>();
    private long id;

    public static void show(Activity activity,long id){
        Intent intent = new Intent(activity,LookLinkWorkItemActivity.class);
        intent.putExtra("id",id);
        activity.startActivity(intent);
    }

    private void setLoadingState(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.GONE){
                    llEmptyMsg.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.GONE){
                    refreshLayout.setVisibility(View.VISIBLE);
                    bottom.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
        }
    }


    @Override
    protected void init() {
        id = getIntent().getLongExtra("id",-1);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_look_link_work_item;
    }

    @Override
    protected LookLinkWorkAtPresenter createPresenter() {
        return new LookLinkWorkAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("查看关联工作项");
        initAdapter();
    }

    /**
     * 设置适配器....
     */
    private void initAdapter() {
        rvPlanItem.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mLookLinkWorkAdapter = new LookLinkWorkAdapter(mLookLinkWorkBeans);
        rvPlanItem.setAdapter(mLookLinkWorkAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryList(id);

    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());
        btnNextRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });

        btnAllotWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkWhichSelected();
            }
        });

        mLookLinkWorkAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.checkbox){
                    boolean selected = mLookLinkWorkBeans.get(position).isSelected();
                    mLookLinkWorkBeans.get(position).setSelected(!selected);
                }else if (view.getId() == R.id.llTime){
                    Integer time =  mLookLinkWorkBeans.get(position).getPredictTime();
                    showSetDieTimeDialog(position,time);
                }
            }
        });

        mLookLinkWorkAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Boolean check = mLookLinkWorkBeans.get(position).getChoose();
                if (check == null || !check){
                    boolean selected = mLookLinkWorkBeans.get(position).isSelected();
                    mLookLinkWorkBeans.get(position).setSelected(!selected);
                    mLookLinkWorkAdapter.notifyItemChanged(position);
                }
            }
        });
    }

    private void showSetDieTimeDialog(int position, Integer time) {
        new SetDieTimeDialog.Builder(this)
                .setPrice(time)
                .setEnsureListener(dieTime -> {
                    mLookLinkWorkBeans.get(position).setPredictTime(dieTime);
                    mLookLinkWorkAdapter.notifyItemChanged(position);
                }).build().show();
    }

    private void checkWhichSelected() {
        MakePlanManRequest makePlanManRequest = new MakePlanManRequest();
        List<EmployeeBean> employeeBeans = new ArrayList<>();
        for (int i = 0;i<mLookLinkWorkBeans.size();i++){
            Log.e("OkHttp",mLookLinkWorkBeans.get(i).getPredictTime()+" " );
            if (mLookLinkWorkBeans.get(i).isSelected()){
                LookLinkWorkBean bean = mLookLinkWorkBeans.get(i);
                employeeBeans.add(new EmployeeBean(
                        bean.getId(),
                        bean.getPredictTime() == null ? 1:bean.getPredictTime(),
                        2
                ));
            }
        }
        if (employeeBeans.size() == 0){
            UIUtils.showToast("请选择员工");
            return;
        }
        makePlanManRequest.setEmployeeList(employeeBeans);
        // 员工ID设置进来
        makePlanManRequest.setEmployeeId(id);
        mPresenter.actionMakePlan(makePlanManRequest);
    }

    @Override
    public void queryWorksSuccess(List<LookLinkWorkBean> list) {
        if (list == null || list.size() == 0){
            setLoadingState(LoadingType.EMPTY);
        }else {
            setLoadingState(LoadingType.SUCCESS);
            mLookLinkWorkBeans.clear();
            mLookLinkWorkBeans.addAll(list);
            mLookLinkWorkAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void queryWorksFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }

    @Override
    public void allotPlanSuccess(String success) {
        UIUtils.showToast(success);
        finish();
    }

    @Override
    public void allotPlanFailure(String err) {
        UIUtils.showToast(err);
    }
}
